/*
Copyright (c) 2008 Ryan BB Schreiber, Brendan G Fry

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using Castle.ActiveRecord;
using log4net;
using System.Xml.Serialization;
using NHibernate.Criterion;

namespace Darkside.GeneratorStudio.Library.Domain
{
    [Serializable]
    public class Project :IDisposable
    {
        private static readonly ILog _Log = LogManager.GetLogger(typeof(Project));

        public string ProjectNameSpace {get; set;}
        public string SolutionNameSpace { get; set; } // Now the solution namespace

        public int ProjectGenerateVersion {get; set;} 

        public bool AllSelected {get; set;}

        private IList<Table> _Tables;
        [XmlIgnore]
        public IList<Table> Tables
        {
            get
            {
                if (_Tables == null)
                {
                    _Tables = ActiveRecordBase<Table>.FindAll(Order.Asc("Name"));
                }
                return (_Tables);
            }
        }

        public string ConnectionString {get; set;}

        public string OutputPath { get; set; } 

        public string SnippetPath {get; set;}

        private readonly List<TemplateFile> _TemplateFiles = new List<TemplateFile>();
        public List<TemplateFile> TemplateFiles
        {
            get { return (_TemplateFiles); }
        }

        public void Refresh()
        {
            //TODO: Do a refresh...
        }


        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private static void Dispose(bool disposing)
        {
            if (disposing)
            {
                //_SqlConnection.Dispose();
            }
        }

        #endregion

        public static void SaveProject (Project project, string fileName)
        {
            if (File.Exists(fileName)) // When overwriting files things go wonky, this should sort it out.
            {
                File.Delete(fileName);
            }

            var fileStream = new FileStream(fileName, FileMode.OpenOrCreate);

            // Construct a XMLSerializer and use it to serialize the data to the stream.
            var serializer = new XmlSerializer(typeof(Project));
            try
            {
                serializer.Serialize(fileStream, project);
            }
            catch (Exception ex)
            {
                _Log.Error(ex.Message, ex);
            }
            finally
            {
                fileStream.Close();
            }
        }

        public static Project LoadProject(string fileName)
        {
            Project project;
            
            // Construct a XMLSerializer and use it to deserialize the data to an object.
            var fileStream = new FileStream(fileName, FileMode.Open);
            try
            {
                var serializer = new XmlSerializer(typeof(Project));
                project = (Project)serializer.Deserialize(fileStream);
                project.Initialise();
            }
            catch (SerializationException ex)
            {
                _Log.Error(ex.Message, ex);
                throw;
            }
            finally
            {
                fileStream.Close();
            }
            return (project);
        }

        public void Initialise()
        {
            ActiveRecordInitialiser.Initialise(ConnectionString);
        }
    }
}
